/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package MuleGame;
package MuleGUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import MuleBackend.GameDriver;
import MuleBackend.Inventory;
import MuleBackend.Player;
import MuleBackend.Stages;
import MuleBackend.Stages.TimeClass;
import MuleBackend.Town;

import java.awt.Font;

import javax.swing.SwingConstants;
/**
* This GUI is a type of land called town in the board
* This GUI include 4 booth, mule store, pub, land sale
* booth and asset booth.
* This GUI is where players interact to buy different
* kind of stores.
* When the player enter the pub, the turn ends.
* This GUI opens when player go to the middle land in 
* the board, and closes when the user clicks on the
* GUI.
*/
public class TownGUI extends javax.swing.JFrame {
	
	

	private JPanel mainPanel;
	
	
	private JLabel crystite, smithore, energy, food, mule, pub,landSale,asset;
	private JLabel waterWay1,waterWay2, landWay;
	private JLabel crystitePrice, smithorePrice, energyPrice, foodPrice, mulePrice;
	
	private final ImageIcon WATER = new ImageIcon("src/images/water1.png");
	private final ImageIcon FOOD = new ImageIcon("src/images/foodBooth1.png");
	private final ImageIcon ENERGY = new ImageIcon("src/images/energyBooth1.png");
	private final ImageIcon SMITHORE = new ImageIcon("src/images/smithoreBooth.png");
	private final ImageIcon CRYSTITE = new ImageIcon("src/images/crystiteBooth.png");
	private final ImageIcon MULE = new ImageIcon("src/images/muleBooth.png");
	private final ImageIcon PUB = new ImageIcon("src/images/pubBooth.png");
	private final ImageIcon LANDSALE = new ImageIcon("src/images/saleBooth.png");
	private final ImageIcon ASSET = new ImageIcon("src/images/assetBooth.png");
	private final ImageIcon LANDWAY = new ImageIcon("src/images/landWay.png");
	

	private final int IMAGE_WIDTH = 100;
	private final int IMAGE_HEIGHT = 100;
	public int x,y,speed,muleX,muleY;
	public Player player;
	public boolean hasMule;

   /**
    * Creates new form StoreGUI
    */
   public TownGUI() {
		setResizable(false);
		getContentPane().setLayout(null);
		hasMule = false;
		x = 10;
		y=300;
		speed = 15;
		buildGUI();
   }
   

	/**
	 * This method create GUI for the Town
	 */
	private void buildGUI(){
		setTitle("Town");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(900,720);
		setLocationRelativeTo(null);
		
		mainPanel = new JPanel();
		mainPanel.setBorder(new EmptyBorder(5,5,5,5));
		setContentPane(mainPanel);
		mainPanel.setLayout(null);
		
		crystite = new JLabel(CRYSTITE);
		crystite.setBounds(0,0,200,300);
		crystite.setBorder(new LineBorder(Color.BLACK));
		smithore = new JLabel(SMITHORE);
		smithore.setBounds(200,0,200,300);
		smithore.setBorder(new LineBorder(Color.BLACK));
		energy = new JLabel(ENERGY);
		energy.setBounds(500,0,200,300);
		energy.setBorder(new LineBorder(Color.BLACK));
		food = new JLabel(FOOD);
		food.setBounds(700,0,200,300);
		food.setBorder(new LineBorder(Color.BLACK));
		waterWay1 = new JLabel(WATER);
		waterWay1.setBounds(400,0,100,300);
		waterWay1.setBorder(new LineBorder(Color.BLACK));
		
		asset = new JLabel(ASSET);
		asset.setBounds(0,400,200,300);
		asset.setBorder(new LineBorder(Color.BLACK));
		landSale = new JLabel(LANDSALE);
		landSale.setBounds(200,400,200,300);
		landSale.setBorder(new LineBorder(Color.BLACK));
		pub = new JLabel(PUB);
		pub.setBounds(500,400,200,300);
		pub.setBorder(new LineBorder(Color.BLACK));
		mule = new JLabel(MULE);
		mule.setBounds(700,400,200,300);
		mule.setBorder(new LineBorder(Color.BLACK));
		waterWay2 = new JLabel(WATER);
		waterWay2.setBounds(400,400,100,300);
		waterWay2.setBorder(new LineBorder(Color.BLACK));
		
		landWay = new JLabel(LANDWAY);
		landWay.setBounds(0,300,900,100);
		landWay.setBorder(new LineBorder(Color.BLACK));
		crystitePrice = new JLabel("$100");
		crystitePrice.setHorizontalAlignment(SwingConstants.CENTER);
		crystitePrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		crystitePrice.setBounds(70,57,91,43);
		smithorePrice = new JLabel("$50");
		smithorePrice.setHorizontalAlignment(SwingConstants.CENTER);
		smithorePrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		smithorePrice.setBounds(270,57,85,43);
		energyPrice = new JLabel("$25");
		energyPrice.setHorizontalAlignment(SwingConstants.CENTER);
		energyPrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		energyPrice.setBounds(570,57,85,43);
		foodPrice = new JLabel("$30");
		foodPrice.setHorizontalAlignment(SwingConstants.CENTER);
		foodPrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		foodPrice.setBounds(777,57,85,43);
		
		mulePrice = new JLabel("$100");
		mulePrice.setHorizontalAlignment(SwingConstants.CENTER);
		mulePrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		mulePrice.setBounds(761, 581, 85, 43);

		getContentPane().add(crystitePrice);
		getContentPane().add(smithorePrice);
		getContentPane().add(energyPrice);
		getContentPane().add(foodPrice);
		getContentPane().add(mulePrice);
		getContentPane().add(crystite);
		getContentPane().add(smithore);
		getContentPane().add(energy);
		getContentPane().add(food);
		getContentPane().add(waterWay1);
		getContentPane().add(landWay);
		getContentPane().add(asset);
		getContentPane().add(landSale);
		getContentPane().add(pub);
		getContentPane().add(mule);
		getContentPane().add(waterWay2);
		
		addKeyListener(new PlayerControl());
		setFocusable(true);
		setFocusTraversalKeysEnabled(false);
	}
	/**
	 * This method create the player
	 */
	public void paint(Graphics g){
		super.paint(g);
		int mule = GameDriver.game.getTown().getMuleStock();
		ImageIcon muleImage = new ImageIcon("src/ShittyPackage/player'sMule.png");
		g.drawImage(player.getRace().getImage(), x, y,IMAGE_WIDTH, IMAGE_HEIGHT, null);
		
		if (GameDriver.game.getCurrentPlayer().getMuleImage() != null)
			g.drawImage(player.getMuleImage().getImage(),muleX,muleY,IMAGE_WIDTH, IMAGE_HEIGHT,null);
		for (int i=1; i<=mule; i++){
			g.drawImage(muleImage.getImage(),870,680-(20*i),20,20,null);
		}
	}
	
	/**
	 * setter method to set whether or not the player has a mule
	 * @param hasMule true or false depending on if the player has a mule
	 */
	public void setMule(boolean hasMule){this.hasMule = hasMule;}
	
	/**
	 * helper method for extra checks to see
	 * if the player has a mule or not
	 */
	public void checkMule() {
		if (player.getMule() == 0) {
			setMule(false);
		}
	}
	
	/**
	 * the location where player start
	 * @param x the coordinate on x-axis
	 * @param y the coordinate on y-axis
	 */
	public void startPlayer(int x, int y){
		this.x = x;
		this.y = y;
		checkMule();
		repaint();
	}
	
	/**
	 * this method move the player using keylistener
	 * @author Spyro
	 *
	 */
	private class PlayerControl implements KeyListener{
		
		/**
		 * controls player movement
		 */
		public void keyPressed(KeyEvent e){
			
			char ch = e.getKeyChar();
			
			if (ch == 'a' || e.getKeyCode() == KeyEvent.VK_LEFT)
				if (x>=0 && isValid(0)){
					x = x - speed;
					muleX = x+IMAGE_WIDTH;
					muleY = y;
					moveToBoard();
			}
			if (ch == 'w' || e.getKeyCode() == KeyEvent.VK_UP)
				if (y>0 && isValid(1)){
					y = y - speed;
					muleY = y+IMAGE_HEIGHT;
					muleX = x;
					moveToBoard();
			}
			if (ch == 'd' || e.getKeyCode() == KeyEvent.VK_RIGHT)
				if (x<810 && isValid(2)){
					x = x + speed;
					muleX = x-IMAGE_WIDTH;
					muleY = y;
					moveToBoard();
			}
			if (ch == 's' || e.getKeyCode() == KeyEvent.VK_DOWN)
				if (y<610 && isValid(3)){
					y = y + speed;
					muleY = y-IMAGE_HEIGHT;
					muleX = x;
					moveToBoard();
			}
			System.out.println("x: "+x+", y: "+y);
			
			if(GameDriver.game.getDifficulty() == "Easy")
			doStuff(e);
			
			else if(GameDriver.game.getDifficulty() == "Medium")
				doMediumStuff(e);
			
			else
				doDifficultStuff(e);
			
			repaint();
		}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
		}
	}
	
	private void gamble(Player player) {
		
 	   	   Player activePlayer = player;

	   	   int roundNumber = GameDriver.game.getRoundNumber();	
	   	   int roundBonus = 0;
 	   	   int timeBonus = 0;
 	   	   int time;
 	   System.out.println(activePlayer.toString());
 	   	
 	   				//Calculate the correct round bonus to be given:
 	   			   {
 	   				   if(roundNumber >= 1 && roundNumber <= 3)
 	   					   roundBonus = 50;
 	   	
 	   				   else if(roundNumber >= 4 && roundNumber <= 7)
 	   					   roundBonus = 100;
 	   	
 	   			   else if(roundNumber >= 8 && roundNumber <= 11)
 	   					   roundBonus = 150;
 	   	
 	   				   else
 	   					   roundBonus = 200;
 	   			   }
 	   	
 	   			time =(int) ((Stages.getTimeAllotted()/2) * (GameDriver.game.getBoardGUI().getTurnPercentRemaining()));
 	   	
 	   		//Calculate the correct timeBonus to be given:
 	   			if(time >= 0 && time <= 12)
 	   			{
 	   				timeBonus = 50;
 	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
 	   		}
 	   	
 	   			if(time >= 13 && time <= 25)
 	   			{
 	   				timeBonus = 100;
 	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
 	   			}
 	   	
 	   			if(time >= 26 && time <= 37)
 	   			{
 	   				timeBonus = 150;
 	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
 	   			}
 	   	
 	   			if(time >= 38 && time <= 50)
 	   			{
 	   				timeBonus = 200;
 	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
 	   			}
 	   	
 	   			int totalMoney = timeBonus + roundBonus;
 	   	
 	   			if(totalMoney >= 250)
 	   				totalMoney = 250;
 	   	
 	   		System.out.println("time is: " + time);
 	   			System.out.println("Round Bonus is: " + roundBonus + " for round " + GameDriver.game.getRoundNumber());
 	   			System.out.println("Time Bonus is: " + timeBonus + " for " + time);
 	   			System.out.println("The player was awarded: " + totalMoney);
 	   			
 	   			
 	   			
 	   		 //Grant the correct end of turn money to the actual correct player
 	   			activePlayer.addMoney(totalMoney);
	}
	
	private void gambleMedium(Player player) {
		
	   	   Player activePlayer = player;

	   	   int roundNumber = GameDriver.game.getRoundNumber();	
	   	   int roundBonus = 0;
	   	   int timeBonus = 0;
	   	   int time;
	   System.out.println(activePlayer.toString());
	   	
	   				//Calculate the correct round bonus to be given:
	   			   {
	   				   if(roundNumber >= 1 && roundNumber <= 3)
	   					   roundBonus = 25;
	   	
	   				   else if(roundNumber >= 4 && roundNumber <= 7)
	   					   roundBonus = 50;
	   	
	   			   else if(roundNumber >= 8 && roundNumber <= 11)
	   					   roundBonus = 75;
	   	
	   				   else
	   					   roundBonus = 100;
	   			   }
	   	
	   			time =(int) ((Stages.getTimeAllotted()/2) * (GameDriver.game.getBoardGUI().getTurnPercentRemaining()));
	   	
	   		//Calculate the correct timeBonus to be given:
	   			if(time >= 0 && time <= 12)
	   			{
	   				timeBonus = 50;
	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
	   		}
	   	
	   			if(time >= 13 && time <= 25)
	   			{
	   				timeBonus = 100;
	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
	   			}
	   	
	   			if(time >= 26 && time <= 37)
	   			{
	   				timeBonus = 150;
	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
	   			}
	   	
	   			if(time >= 38 && time <= 50)
	   			{
	   				timeBonus = 200;
	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
	   			}
	   	
	   			int totalMoney = timeBonus + roundBonus;
	   	
	   			if(totalMoney >= 200)
	   				totalMoney = 200;
	   	
	   		System.out.println("time is: " + time);
	   			System.out.println("Round Bonus is: " + roundBonus + " for round " + GameDriver.game.getRoundNumber());
	   			System.out.println("Time Bonus is: " + timeBonus + " for " + time);
	   			System.out.println("The player was awarded: " + totalMoney);
	   			
	   			
	   			
	   		 //Grant the correct end of turn money to the actual correct player
	   			activePlayer.addMoney(totalMoney);
	}
	
	private void gambleDifficult(Player player) {
		
	   	   Player activePlayer = player;

	   	   int roundNumber = GameDriver.game.getRoundNumber();	
	   	   int roundBonus = 0;
	   	   int timeBonus = 0;
	   	   int time;
	   System.out.println(activePlayer.toString());
	   	
	   				//Calculate the correct round bonus to be given:
	   			   {
	   				   if(roundNumber >= 1 && roundNumber <= 3)
	   					   roundBonus = 25;
	   	
	   				   else if(roundNumber >= 4 && roundNumber <= 7)
	   					   roundBonus = 50;
	   	
	   			   else if(roundNumber >= 8 && roundNumber <= 11)
	   					   roundBonus = 75;
	   	
	   				   else
	   					   roundBonus = 100;
	   			   }
	   	
	   			time =(int) ((Stages.getTimeAllotted()/2) * (GameDriver.game.getBoardGUI().getTurnPercentRemaining()));
	   	
	   		//Calculate the correct timeBonus to be given:
	   			if(time >= 0 && time <= 12)
	   			{
	   				timeBonus = 25;
	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
	   		}
	   	
	   			if(time >= 13 && time <= 25)
	   			{
	   				timeBonus = 50;
	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
	   			}
	   	
	   			if(time >= 26 && time <= 37)
	   			{
	   				timeBonus = 75;
	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
	   			}
	   	
	   			if(time >= 38 && time <= 50)
	   			{
	   				timeBonus = 100;
	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
	   			}
	   	
	   			int totalMoney = timeBonus + roundBonus;
	   	
	   			if(totalMoney >= 150)
	   				totalMoney = 150;
	   	
	   		System.out.println("time is: " + time);
	   			System.out.println("Round Bonus is: " + roundBonus + " for round " + GameDriver.game.getRoundNumber());
	   			System.out.println("Time Bonus is: " + timeBonus + " for " + time);
	   			System.out.println("The player was awarded: " + totalMoney);
	   			
	   			
	   			
	   		 //Grant the correct end of turn money to the actual correct player
	   			activePlayer.addMoney(totalMoney);
	}
	
   /**
    * This method contains logic for things that will happen within the town.
    * 
    * 1) When the pub is entered, an amount of money is calculated for the current player based
    * on their time remaining, and the current player's turn ends. The game then advances 
    * to the next stage. 
    * 
    * 2) When the mule store is entered, if the player has enough money for a mule, one is
    * purchased.
    * 
    * 3) When a player enters a store and has enough money for that mule factory outfit, one
    * is added to the mule for that corresponding store type.
    * 
    * 4) If a player presses z or x, they will buy or sell the respective good accordingly.
    *    
    * 
    */
	 
	public void doStuff(KeyEvent e){
		if (x<195 && y>=500){
			System.out.println("this is asset");
		}
		if (x>=195 && x<=400 && y>=500){
			System.out.println("this is land sale");
		}
		if (x>=495 && x<695 && y>=500){
			System.out.println("this is pub");			   
			   //calculate how much money the player gets
	     		gamble(player);
	     		
	     		System.out.println("Player" + player + 
	    		 "'s current money: "+ player.getMoney());
	     		
	     		//stop the turn
	 	   		//GameDriver.game.boardGUI.endTurn();
	 	   		GameDriver.game.endTimer();
	     }
		if (x>=695 && y>=500 && !hasMule){
			System.out.println("this is mule");
			if (player.hasMoney(Town.MULE_PRICE)){
				player.deductMoney(Town.MULE_PRICE);
				GameDriver.game.getTown().setMuleStock(-1);
				hasMule = true;
				player.setMule(1);
			}
		}
		if (x<195 && y<=100){
			System.out.println("this is crystite");
			if (hasMule && player.hasMoney(Town.CRYSTITE_FIT_PRICE)){
				player.deductMoney(Town.CRYSTITE_FIT_PRICE);
				hasMule = false;
				player.setMule(5);
			}
			if (e.getKeyChar() == 'x') {
				if (player.getCrystite() > 0) {
					player.addMoney(Town.CRYSTITE_FIT_PRICE);
					player.addCrystite(-1);
					GameDriver.game.updateInfoPanels();
				}
			}
			
		}
		if (x>=195 && x<=400 && y<=100){
			System.out.println("this is smithore");
			if (hasMule && player.hasMoney(Town.SMITHORE_FIT_PRICE)){
				player.deductMoney(Town.SMITHORE_FIT_PRICE);
				hasMule = false;
				player.setMule(4);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentSmithorePrice() && Town.getSmithoreStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentSmithorePrice());
					 player.addSmithore(1);
					 GameDriver.game.getTown().setSmithoreStock(-1);
					 GameDriver.game.getTown().updateSmithorePrice(6);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getSmithore() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentSmithorePrice());
					player.addSmithore(-1);
					GameDriver.game.getTown().setSmithoreStock(1);
					GameDriver.game.getTown().updateSmithorePrice(-5);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
		
		if (x>=495 && x<695 && y<=100){
			System.out.println("this is energy");
			if (hasMule &&player.hasMoney(Town.ENERGY_FIT_PRICE)){
				player.deductMoney(Town.ENERGY_FIT_PRICE);
				hasMule = false;
				player.setMule(3);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentEnergyPrice() && Town.getEnergyStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentEnergyPrice());
					 player.addEnergy(1);
					 GameDriver.game.getTown().setEnergyStock(-1);
					 GameDriver.game.getTown().updateEnergyPrice(6);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getEnergy() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentEnergyPrice());
					player.addEnergy(-1);
					GameDriver.game.getTown().setEnergyStock(1);
					GameDriver.game.getTown().updateEnergyPrice(-5);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
		if (x>=695 && y<=100){
			System.out.println("this is food");
			if (hasMule && player.hasMoney(Town.FOOD_FIT_PRICE)){
				player.deductMoney(Town.FOOD_FIT_PRICE);
				hasMule = false;
				player.setMule(2);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentFoodPrice() && Town.getFoodStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentFoodPrice());
					 player.addFood(1);
					 GameDriver.game.getTown().setFoodStock(-1);
					 GameDriver.game.getTown().updateFoodPrice(6);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getFood() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentFoodPrice());
					player.addFood(-1);
					GameDriver.game.getTown().setFoodStock(1);
					GameDriver.game.getTown().updateFoodPrice(-5);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
	}
	
	public void doMediumStuff(KeyEvent e){
		if (x<195 && y>=500){
			System.out.println("this is asset");
		}
		if (x>=195 && x<=400 && y>=500){
			System.out.println("this is land sale");
		}
		if (x>=495 && x<695 && y>=500){
			System.out.println("this is pub");			   
			   //calculate how much money the player gets
	     		gambleMedium(player);
	     		
	     		System.out.println("Player" + player + 
	    		 "'s current money: "+ player.getMoney());
	     		
	     		//stop the turn
	 	   		//GameDriver.game.boardGUI.endTurn();
	 	   		GameDriver.game.endTimer();
	     }
		if (x>=695 && y>=500 && !hasMule){
			System.out.println("this is mule");
			if (player.hasMoney(Town.MULE_PRICE)){
				player.deductMoney(Town.MULE_PRICE);
				GameDriver.game.getTown().setMuleStock(-1);
				hasMule = true;
				player.setMule(1);
			}
		}
		if (x<195 && y<=100){
			System.out.println("this is crystite");
			if (hasMule && player.hasMoney(Town.CRYSTITE_FIT_PRICE)){
				player.deductMoney(Town.CRYSTITE_FIT_PRICE);
				hasMule = false;
				player.setMule(5);
			}
			if (e.getKeyChar() == 'x') {
				if (player.getCrystite() > 0) {
					player.addMoney(Town.CRYSTITE_FIT_PRICE);
					player.addCrystite(-1);
					GameDriver.game.updateInfoPanels();
				}
			}
			
		}
		if (x>=195 && x<=400 && y<=100){
			System.out.println("this is smithore");
			if (hasMule && player.hasMoney(Town.SMITHORE_FIT_PRICE)){
				player.deductMoney(Town.SMITHORE_FIT_PRICE);
				hasMule = false;
				player.setMule(4);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentSmithorePrice() && Town.getSmithoreStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentSmithorePrice());
					 player.addSmithore(1);
					 GameDriver.game.getTown().setSmithoreStock(-1);
					 GameDriver.game.getTown().updateSmithorePrice(12);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getSmithore() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentSmithorePrice());
					player.addSmithore(-1);
					GameDriver.game.getTown().setSmithoreStock(1);
					GameDriver.game.getTown().updateSmithorePrice(-10);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
		
		if (x>=495 && x<695 && y<=100){
			System.out.println("this is energy");
			if (hasMule &&player.hasMoney(Town.ENERGY_FIT_PRICE)){
				player.deductMoney(Town.ENERGY_FIT_PRICE);
				hasMule = false;
				player.setMule(3);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentEnergyPrice() && Town.getEnergyStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentEnergyPrice());
					 player.addEnergy(1);
					 GameDriver.game.getTown().setEnergyStock(-1);
					 GameDriver.game.getTown().updateEnergyPrice(12);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getEnergy() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentEnergyPrice());
					player.addEnergy(-1);
					GameDriver.game.getTown().setEnergyStock(1);
					GameDriver.game.getTown().updateEnergyPrice(-10);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
		if (x>=695 && y<=100){
			System.out.println("this is food");
			if (hasMule && player.hasMoney(Town.FOOD_FIT_PRICE)){
				player.deductMoney(Town.FOOD_FIT_PRICE);
				hasMule = false;
				player.setMule(2);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentFoodPrice() && Town.getFoodStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentFoodPrice());
					 player.addFood(1);
					 GameDriver.game.getTown().setFoodStock(-1);
					 GameDriver.game.getTown().updateFoodPrice(12);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getFood() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentFoodPrice());
					player.addFood(-1);
					GameDriver.game.getTown().setFoodStock(1);
					GameDriver.game.getTown().updateFoodPrice(-10);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
	}
	
	public void doDifficultStuff(KeyEvent e){
		if (x<195 && y>=500){
			System.out.println("this is asset");
		}
		if (x>=195 && x<=400 && y>=500){
			System.out.println("this is land sale");
		}
		if (x>=495 && x<695 && y>=500){
			System.out.println("this is pub");			   
			   //calculate how much money the player gets
	     		gambleDifficult(player);
	     		
	     		System.out.println("Player" + player + 
	    		 "'s current money: "+ player.getMoney());
	     		
	     		//stop the turn
	 	   		//GameDriver.game.boardGUI.endTurn();
	 	   		GameDriver.game.endTimer();
	     }
		if (x>=695 && y>=500 && !hasMule){
			System.out.println("this is mule");
			if (player.hasMoney(Town.MULE_PRICE)){
				player.deductMoney(Town.MULE_PRICE);
				GameDriver.game.getTown().setMuleStock(-1);
				hasMule = true;
				player.setMule(1);
			}
		}
		if (x<195 && y<=100){
			System.out.println("this is crystite");
			if (hasMule && player.hasMoney(Town.CRYSTITE_FIT_PRICE)){
				player.deductMoney(Town.CRYSTITE_FIT_PRICE);
				hasMule = false;
				player.setMule(5);
			}
			if (e.getKeyChar() == 'x') {
				if (player.getCrystite() > 0) {
					player.addMoney(Town.CRYSTITE_FIT_PRICE);
					player.addCrystite(-1);
					GameDriver.game.updateInfoPanels();
				}
			}
			
		}
		if (x>=195 && x<=400 && y<=100){
			System.out.println("this is smithore");
			if (hasMule && player.hasMoney(Town.SMITHORE_FIT_PRICE)){
				player.deductMoney(Town.SMITHORE_FIT_PRICE);
				hasMule = false;
				player.setMule(4);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentSmithorePrice() && Town.getSmithoreStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentSmithorePrice());
					 player.addSmithore(1);
					 GameDriver.game.getTown().setSmithoreStock(-1);
					 GameDriver.game.getTown().updateSmithorePrice(18);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getSmithore() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentSmithorePrice());
					player.addSmithore(-1);
					GameDriver.game.getTown().setSmithoreStock(1);
					GameDriver.game.getTown().updateSmithorePrice(-15);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
		
		if (x>=495 && x<695 && y<=100){
			System.out.println("this is energy");
			if (hasMule &&player.hasMoney(Town.ENERGY_FIT_PRICE)){
				player.deductMoney(Town.ENERGY_FIT_PRICE);
				hasMule = false;
				player.setMule(3);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentEnergyPrice() && Town.getEnergyStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentEnergyPrice());
					 player.addEnergy(1);
					 GameDriver.game.getTown().setEnergyStock(-1);
					 GameDriver.game.getTown().updateEnergyPrice(18);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getEnergy() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentEnergyPrice());
					player.addEnergy(-1);
					GameDriver.game.getTown().setEnergyStock(1);
					GameDriver.game.getTown().updateEnergyPrice(-15);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
		if (x>=695 && y<=100){
			System.out.println("this is food");
			if (hasMule && player.hasMoney(Town.FOOD_FIT_PRICE)){
				player.deductMoney(Town.FOOD_FIT_PRICE);
				hasMule = false;
				player.setMule(2);
			}
			if (e.getKeyChar() == 'z') {
				 GameDriver.game.getTown();
				if (player.getMoney() > GameDriver.game.getTown().getCurrentFoodPrice() && Town.getFoodStock() > 0) {
					 player.deductMoney(GameDriver.game.getTown().getCurrentFoodPrice());
					 player.addFood(1);
					 GameDriver.game.getTown().setFoodStock(-1);
					 GameDriver.game.getTown().updateFoodPrice(18);
					 GameDriver.game.updateInfoPanels();
				 }
			}
			if (e.getKeyChar() == 'x') {
				if (player.getFood() > 0) {
					player.addMoney(GameDriver.game.getTown().getCurrentFoodPrice());
					player.addFood(-1);
					GameDriver.game.getTown().setFoodStock(1);
					GameDriver.game.getTown().updateFoodPrice(-15);
					GameDriver.game.updateInfoPanels();
				}
			}
		}
	}

   
   public void setPlayer(Player player) {
		this.player = player;
	}
   
	/**
	 * this method check if the next location is valid
	 * for the player to move
	 * @param i the direction represented by 0-3
	 * @return true if the location is valid and 
	 * player's location isn't going out of townGUI
	 * false otherwise
	 */
	public boolean isValid(int i){
		int x1 = x;
		int y1 = y;
		switch (i){
		case 0:
			if (x>=0)x1 = x1 - speed;
			break;
		case 1:
			if (y>5)y1 = y1 - speed;
			break;
		case 2:
			if (x<=800)x1 = x1 + speed;
			break;
		case 3:
			if (y<610)y1 = y1 + speed;
			break;
		}
		if (x1 <=0 && y1<=290)return false;
		if (x1 >=800 && y1<=290)return false;
		if (x1 >=130 && x1<=190 && y1<=290)return false;
		if (x1 >=325 && x1<=390 && y1<=290)return false;
		if (x1 >=430 && x1<=490 && y1<=290)return false;
		if (x1 >=630 && x1<=690 && y1<=290)return false;
		if (x1 >=130 && x1<=190 && y1>=340)return false;
		if (x1 >=325 && x1<=390 && y1>=340)return false;
		if (x1 >=430 && x1<=490 && y1>=340)return false;
		if (x1 >=630 && x1<=690 && y1>=340)return false;
		return true;
	}
	
	/**
	 * this method move the player to the board
	 * when the player location exit the townGUI
	 */
	public void moveToBoard(){
		if (x<0 && y>=250 && y<=350){
			GameDriver.game.boardGUI.startPlayer(350,275);
			setVisible(false);
			System.out.println("x is :" + x);
			System.out.println("y is :" + y);
			System.out.println("statement 1");
		}
		else if (y<=10 && x>=350 && x<=450){
			setVisible(false);
			System.out.println("x is :" + x);
			System.out.println("y is :" + y);
			GameDriver.game.boardGUI.startPlayer(425,190);
			System.out.println("statement 2");
		}
		else if (x>805 && y>=250 && y<=350){
			setVisible(false);
			System.out.println("x is :" + x);
			System.out.println("y is :" + y);
			GameDriver.game.boardGUI.startPlayer(500,275);
			System.out.println("statement 3");
		}
		else if (y>=600 && x>=350 && x<=450){
			setVisible(false);
			System.out.println("x is :" + x);
			System.out.println("y is :" + y);
			GameDriver.game.boardGUI.startPlayer(425,325);
			System.out.println("statement 4");
		}
	}

}
